package com.juli.school.service.impl;

import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.juli.school.utils.oauth.Oauth;
import com.juli.school.utils.oauth.OauthQQ;
import com.juli.school.utils.oauth.OauthSina;
import com.juli.school.utils.oauth.OauthWeixin;
import com.juli.school.utils.oauth.TokenUtil;

@Service
@PropertySource("classpath:config/oauth.properties")
public class OauthService {
	
	OauthSina oauthSina = OauthSina.me();
	
	OauthQQ oauthQQ = OauthQQ.me();
	
	OauthWeixin oauthWeixin = OauthWeixin.me();
	
	public String getOauthUrl(String source) {
		String authorizeUrl = getOauth(source).getAuthorizeUrl(TokenUtil.randomState());
		return authorizeUrl;
	}

	public JSONObject callback(String source,String code) {
		return getOauth(source).getUserInfoByCode(code);
	}
	private Oauth getOauth(String source) {
		if("qq".equals(source)) {
			return oauthQQ;
		} else if("weixin".equals(source)) {
			return oauthWeixin;
		} else if("sina".equals(source)) {
			return oauthSina;
		}
		return null;
	}
	
}
